<!doctype html>
<html lang="zh-CH">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<title>钢链网--注册</title>
	<link rel="stylesheet" href="/resource/css/gl_index.css">
	<link rel="stylesheet" href="/resource/css/gl_login.css">

	<script src="/resource/js/jquery-1.11.3.min.js"></script>
	<script>
		$(function(){
			$('input.form-control').blur(function(){
				if ($(this).val() == '') {
					$(this).siblings('span').show().html('不能为空');
					return false;
				}else{
					if ($(this).attr('id') == "mobile") {
						check_mobile();
					}else{
                        $(this).siblings('span').hide();
						return true;
					};

				};
			});
			$(document).delegate('.btn-login','click',function(){
				for (var i = 0; i < $('input.form-control').length; i++) {
					if($('input.form-control').eq(i).val() == ''){
						$('input.form-control').eq(i).focus().siblings('span').show().html('不能为空');
						return false;
					};
					return check_captcha()&&check_news()&&check_agree()&&check_mobile();
				};
			});
			//看不清 点击刷新
			$(document).delegate('.code-img a','click',function(){
				$(this).siblings('img').attr('src',"/captcha?'+ Math.random()");
			});
			//获取短信验证码

			$('#f_captcha').bind('click',function foo(){
				var mobile = document.getElementById('mobile').value;
				var reg = /^1[34578]\d{9}$/;
				$("#t_news").show();
				if(reg.test(mobile)) {
                    console.log(check_mobile())
					if(check_mobile()){
						if(check_captcha()) {
							var wait = 60;
							var int = function fetch_captcha() {
								if (wait == 0) {
									$("#f_captcha").bind("click", foo);
									$('#f_captcha').html('获取验证码').addClass('btn-code-zeor');
									clearInterval(a)
									wait = 60;
								}
								else {
									$('#f_captcha').unbind("click", foo);
									$('#f_captcha').html("重新发送(" + wait + ")").removeClass('btn-code-zeor');
									wait--;
								}
							}
							var a = setInterval(int, 1000);
							$.ajax({
								url: "/verify_news",
								type: "POST",
								data: "mobile=" + mobile,
								success: function (msg) {
									if (msg == 10000) {
										$("#t_news").html("验证码发送成功,10分钟内输入有效");
									}else if(msg == 160040){
										$("#t_news").html("您当日验证码发送次数过多");
										window.clearInterval(a);//停止计时器
										$(".form-code span.code").removeAttr("disabled").css('cursor','pointer');//启用按钮
										$(".form-code span.code").html("获取短信验证码");
										return false;
									}else{
										$("#t_news").html("验证码发送错误");
										window.clearInterval(a);//停止计时器
										$(".form-code span.code").removeAttr("disabled").css('cursor','pointer');//启用按钮
										$(".form-code span.code").html("获取短信验证码");
										return false;
									}
								}
							});
						}else{
							$("#t_captcha").show();
							return false
						}
					}else{
                        $("#t_news").html("该手机号已经注册");
						return false
					}
				}else{
					$("#t_news").html("请输入正确的手机号");
					return false
				}
			})
		});
	//手机号验证
	function check_mobile(){
		var mobile = $("#mobile").val();
		var reg = /^1[34578]\d{9}$/;
		var status = false;
		if(reg.test(mobile)){
			$.ajax({
                'url':'/js_verify_register',
                'data':'mobile='+mobile,
                'type':'POST',
                'async':false,
				 success:function(data){
                    if (data == 0) {
                        $("#mobile").siblings('span').hide();
                        status=true
                    } else {
                        $("#mobile").siblings('span').show().html('该手机号已经注册');
                    }
				 }
			});
            console.log(status);
			return status;
		}else{
			$("#mobile").siblings('span').show().html('请输入正确的手机号');
			return false;
		};
	}

	//验证码验证
	function check_captcha(){
		var captcha =  document.getElementById('captcha').value;
		var status = false;
		if(captcha){
			var ajax = new XMLHttpRequest();
			ajax.onreadystatechange = function () {
				if (ajax.readyState == 4) {
					if (ajax.responseText == 1) {
						$("#t_captcha").hide();
						status = true
					} else {
						$("#t_captcha").show();
					}
				}
			}
			//与服务器建立连接
			ajax.open('post', '/js_verify_register', false);
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			//处理请求
			ajax.send('captcha=' + captcha);
			return status
		}else{
			return false;
		}
	}

	//短信验证
	function check_news(){
		var news_captcha = document.getElementById('news_captcha').value;
		var mobile = document.getElementById('mobile').value;
		$("#t_news").show();
		var status = false;
		if(news_captcha){
			var ajax = new XMLHttpRequest();
			ajax.onreadystatechange = function () {
				if (ajax.readyState == 4) {
					if (ajax.responseText == 1) {
						$("#t_news").hide();
						status = true
					}else if(ajax.responseText == 2){
						$("#t_news").html("验证码已过期");
					}else{
						$("#t_news").html("验证码错误");
					}
				}
			}
			//与服务器建立连接
			ajax.open('post', '/js_verify_register', false);
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			//处理请求
			ajax.send('news_captcha=' + news_captcha + '&mobile=' +mobile);
			return status
		}else{
			document.getElementById('t_news').innerHTML = "<span style='color:red'>*</span>";
			return false;
		}

	}

	//复选框
	function check_agree()
	{
		$('#t_agree').show();
		var agree = document.getElementById('agree');
		if(agree.checked){
			$("#t_agree").html("已阅读");
			return true
		}else{
			$("#t_agree").html("请阅读");
			return false
		}
	}
	</script>
</head>
<body>
<div class="head">
    @include('common.user_head')
</div>
<div class="login-head">
	<div class="main">
		<div class="logo">
			<a href="/"><img src="/resource/img/logo.png" alt=""></a>
			<span>欢迎注册</span>
		</div>
	</div>
</div>	
<div class="register-body main">
	<div class="left">
		<form action="/depose_register" method="post" class="form">
			<div class="form-group">
				<label for=""><i>*</i>手机号</label>
				<input type="text" id="mobile" name="mobile" class="form-control" placeholder="请输入真实有效的手机号">
				<span></span>
			</div>
			<div class="form-group form-code">
				<label for=""><i>*</i>验证码</label>
				<input type="text" class="form-control"  name="captcha" id="captcha" onblur="check_captcha()" placeholder="请输入图形验证码">
				<div class="code-img">
					<img onclick="this.src='/captcha?'+ Math.random()"  src="/captcha">
					<a href="javascript:;">看不清？换一张</a>
				</div>
				<span id="t_captcha">验证码错误</span>
			</div>
			<div class="form-group form-code">
				<label for=""><i>*</i>短信验证码</label>
				<input type="text" class="form-control" onblur="check_news()" id="news_captcha" placeholder="请输入短信验证码">
				<button type="button" class="btn-code" id="f_captcha">获取验证码</button>
				<span id="t_news"></span>
			</div>
			<div class="form-group">
				<label for=""><i>*</i>设置密码</label>
				<input type="password" name="password" class="form-control" placeholder="请输入6-18位密码">
				<span></span>
			</div>
			<div class="form-group">
				<label for=""><i>*</i>确认密码</label>
				<input type="password" name="password" class="form-control" placeholder="请再次输入密码">
				<span></span>
			</div>
			<div class="form-group form-left">
				<input type="checkbox" onclick="check_agree()" id="agree" checked="checked"> 同意并阅读
				<a href="javascript:;" class="a-agree">《钢链网用户服务协议》</a>
				<span id="t_agree"></span>
			</div>
			<button type="submit" class="btn-login">立即注册</button>
		</form>
	</div>
	<div class="right">
		<div class="reg-top">
			<div class="lo">
				<span>已有账号？ 请</span>
				<a href="/login">登录</a>
			</div>
		</div>
		<div class="reg-bot">
			<p>若有任何问题，请联系客服</p>
			<div class="tel"><i></i>400-4562-1225</div>
		</div>
	</div>
</div>
<div class="login-foot main">
	<div class="foot-bot foot-info">
		<ul>
			<li><a href="javascript:;">关于我们</a><span>|</span></li>
			<li><a href="javascript:;">法律声明</a><span>|</span></li>
			<li><a href="javascript:;">诚聘英才</a><span>|</span></li>
			<li><a href="javascript:;">投资洽谈</a><span>|</span></li>
			<li><a href="javascript:;">联系我们</a><span>|</span></li>
			<li><a href="javascript:;">常见问题</a><span></span></li>
		</ul>
		<p>Copyright &copy; 2015.Company name All rights reserved.More Templates <a href="#">苏ICP备15005239号-2</a></p>
	</div>
</div>
</body>
</html>